//
//  UserModel.swift
//  WEIBO
//
//  Created by 谢曦 on 2017/5/6.
//  Copyright © 2017年 谢曦. All rights reserved.
//

import UIKit
import YYModel
class AccountModel: NSObject,NSCoding {
//    "access_token" = "2.00eGrUPDtfJTHD19adcf59580cqcd8";
//    "expires_in" = 157679999;
//    "remind_in" = 157679999;
//    uid = 2977397520;

    var access_token : String?
    var uid : String?
    var expires_in : TimeInterval = 0{
        didSet{
        expires_date = Date.init(timeIntervalSinceNow: expires_in)
        }
    }
    var expires_date: Date?
    
    
    static let accountInstance: AccountModel = {
        
        //接档
        let userInstance = AccountModel()
        let file  = XXFileManager.filePath(with: .document, file:  "acount.data")
        if let file = file,
            let userM = NSKeyedUnarchiver.unarchiveObject(withFile: file) as? AccountModel
        {
            return userM
        }
        
        return userInstance
    }()
    

       // MARK: - YYmodel归档
    func encode(with aCoder: NSCoder) {
    self.yy_modelEncode(with: aCoder)
    }
    
    override func yy_modelDescription() -> String {
        return yy_modelDescription()
    }
    
    //注意！！！实现了这个方法  要重写init方法 
    required init?(coder aDecoder: NSCoder) {
    super.init()
    self.yy_modelInit(with: aDecoder)
    }
    override init() {
        super.init()
    }
}

extension AccountModel{
 // MARK: - 归档
    
  func saveUser(userDict:[String:AnyObject]) -> (){
      yy_modelSet(with: userDict)//字典转模型
    accountInstance = self
       let file  = XXFileManager.filePath(with: .document, file:  "user.data")

        if NSKeyedArchiver.archiveRootObject(self, toFile: file!) == true {
        print("归档成功 \(file)")
        }
    
    }

}
